﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:workplan
	/// </summary>
	public partial class workplan
	{
		public workplan()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("workplan_id", "workplan"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int workplan_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from workplan");
			strSql.Append(" where workplan_id=@workplan_id");
			SqlParameter[] parameters = {
					new SqlParameter("@workplan_id", SqlDbType.Int,4)
			};
			parameters[0].Value = workplan_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.workplan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into workplan(");
			strSql.Append("workplan_title,workplan_content,workplan_type,submit_time,submit_uid,isCheck,isSubmit,check_step)");
			strSql.Append(" values (");
			strSql.Append("@workplan_title,@workplan_content,@workplan_type,@submit_time,@submit_uid,@isCheck,@isSubmit,@check_step)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@workplan_title", SqlDbType.VarChar,200),
					new SqlParameter("@workplan_content", SqlDbType.VarChar,-1),
					new SqlParameter("@workplan_type", SqlDbType.VarChar,20),
					new SqlParameter("@submit_time", SqlDbType.DateTime),
					new SqlParameter("@submit_uid", SqlDbType.Int,4),
					new SqlParameter("@isCheck", SqlDbType.Bit,1),
					new SqlParameter("@isSubmit", SqlDbType.Bit,1),
					new SqlParameter("@check_step", SqlDbType.VarChar,50)};
			parameters[0].Value = model.workplan_title;
			parameters[1].Value = model.workplan_content;
			parameters[2].Value = model.workplan_type;
			parameters[3].Value = model.submit_time;
			parameters[4].Value = model.submit_uid;
			parameters[5].Value = model.isCheck;
			parameters[6].Value = model.isSubmit;
			parameters[7].Value = model.check_step;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.workplan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update workplan set ");
			strSql.Append("workplan_title=@workplan_title,");
			strSql.Append("workplan_content=@workplan_content,");
			strSql.Append("workplan_type=@workplan_type,");
			strSql.Append("submit_time=@submit_time,");
			strSql.Append("submit_uid=@submit_uid,");
			strSql.Append("isCheck=@isCheck,");
			strSql.Append("isSubmit=@isSubmit,");
			strSql.Append("check_step=@check_step");
			strSql.Append(" where workplan_id=@workplan_id");
			SqlParameter[] parameters = {
					new SqlParameter("@workplan_title", SqlDbType.VarChar,200),
					new SqlParameter("@workplan_content", SqlDbType.VarChar,-1),
					new SqlParameter("@workplan_type", SqlDbType.VarChar,20),
					new SqlParameter("@submit_time", SqlDbType.DateTime),
					new SqlParameter("@submit_uid", SqlDbType.Int,4),
					new SqlParameter("@isCheck", SqlDbType.Bit,1),
					new SqlParameter("@isSubmit", SqlDbType.Bit,1),
					new SqlParameter("@check_step", SqlDbType.VarChar,50),
					new SqlParameter("@workplan_id", SqlDbType.Int,4)};
			parameters[0].Value = model.workplan_title;
			parameters[1].Value = model.workplan_content;
			parameters[2].Value = model.workplan_type;
			parameters[3].Value = model.submit_time;
			parameters[4].Value = model.submit_uid;
			parameters[5].Value = model.isCheck;
			parameters[6].Value = model.isSubmit;
			parameters[7].Value = model.check_step;
			parameters[8].Value = model.workplan_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int workplan_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from workplan ");
			strSql.Append(" where workplan_id=@workplan_id");
			SqlParameter[] parameters = {
					new SqlParameter("@workplan_id", SqlDbType.Int,4)
			};
			parameters[0].Value = workplan_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string workplan_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from workplan ");
			strSql.Append(" where workplan_id in ("+workplan_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.workplan GetModel(int workplan_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 workplan_id,workplan_title,workplan_content,workplan_type,submit_time,submit_uid,isCheck,isSubmit,check_step from workplan ");
			strSql.Append(" where workplan_id=@workplan_id");
			SqlParameter[] parameters = {
					new SqlParameter("@workplan_id", SqlDbType.Int,4)
			};
			parameters[0].Value = workplan_id;

			Maticsoft.Model.workplan model=new Maticsoft.Model.workplan();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.workplan DataRowToModel(DataRow row)
		{
			Maticsoft.Model.workplan model=new Maticsoft.Model.workplan();
			if (row != null)
			{
				if(row["workplan_id"]!=null && row["workplan_id"].ToString()!="")
				{
					model.workplan_id=int.Parse(row["workplan_id"].ToString());
				}
				if(row["workplan_title"]!=null)
				{
					model.workplan_title=row["workplan_title"].ToString();
				}
				if(row["workplan_content"]!=null)
				{
					model.workplan_content=row["workplan_content"].ToString();
				}
				if(row["workplan_type"]!=null)
				{
					model.workplan_type=row["workplan_type"].ToString();
				}
				if(row["submit_time"]!=null && row["submit_time"].ToString()!="")
				{
					model.submit_time=DateTime.Parse(row["submit_time"].ToString());
				}
				if(row["submit_uid"]!=null && row["submit_uid"].ToString()!="")
				{
					model.submit_uid=int.Parse(row["submit_uid"].ToString());
				}
				if(row["isCheck"]!=null && row["isCheck"].ToString()!="")
				{
					if((row["isCheck"].ToString()=="1")||(row["isCheck"].ToString().ToLower()=="true"))
					{
						model.isCheck=true;
					}
					else
					{
						model.isCheck=false;
					}
				}
				if(row["isSubmit"]!=null && row["isSubmit"].ToString()!="")
				{
					if((row["isSubmit"].ToString()=="1")||(row["isSubmit"].ToString().ToLower()=="true"))
					{
						model.isSubmit=true;
					}
					else
					{
						model.isSubmit=false;
					}
				}
				if(row["check_step"]!=null)
				{
					model.check_step=row["check_step"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select workplan_id,workplan_title,workplan_content,workplan_type,submit_time,submit_uid,isCheck,isSubmit,check_step ");
			strSql.Append(" FROM workplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" workplan_id,workplan_title,workplan_content,workplan_type,submit_time,submit_uid,isCheck,isSubmit,check_step ");
			strSql.Append(" FROM workplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM workplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.workplan_id desc");
			}
			strSql.Append(")AS Row, T.*  from workplan T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "workplan";
			parameters[1].Value = "workplan_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByUserWorkPlan(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VUserWorkPlan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}

